package org.leo.dm.component.flow;

import java.util.Map;

import org.leo.dm.common.bean.ResultBean;
import org.leo.dm.component.base.IFlow;
import org.leo.dm.component.engine.Storage;
import org.leo.dm.component.reader.Reader;
import org.leo.dm.component.writer.Writer;

/**
 * 
 * Created on 2016年2月17日
 * <p>
 * Description 流程抽象类
 * <p>
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h
 *
 */
public abstract class Flow implements IFlow {

	private Reader reader;
	private Writer writer;
	private Storage storage;

	private boolean isErrorFlag;// 该流程是否发生错误

	/**
	 * @return the isErrorFlag
	 */
	public boolean isErrorFlag() {
		return isErrorFlag;
	}

	/**
	 * @param isErrorFlag
	 *            the isErrorFlag to set
	 */
	public void setErrorFlag(boolean isErrorFlag) {
		this.isErrorFlag = isErrorFlag;
	}

	public Reader getReader() {
		return reader;
	}

	public void setReader(Reader reader) {
		this.reader = reader;
		this.reader.setFlow(this);
	}

	public Writer getWriter() {
		return writer;
	}

	public void setWriter(Writer writer) {
		this.writer = writer;
		this.writer.setFlow(this);
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public ResultBean handle(Map<String, Object> param) throws Exception {
		return null;
	}

	/**
	 * 
	 * Created on 2015年12月31日 .
	 * 
	 * @author xiao_h
	 * @return 流程终止 通知读取组件和接收组件停止运行
	 */
	public String stop() throws Exception {
		this.setErrorFlag(true);

		if (storage != null) {
			storage.putError();
		}
		if (reader != null) {
			reader.setRunFlag(false);
		}
		if (writer != null) {
			writer.setRunFlag(false);
		}
		return null;
	}

	public String run() {
		reader.handle();
		writer.handle();
		return null;
	}

}
